package com.example.sqlitetest.Activity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.sqlitetest.R;
import com.example.sqlitetest.db.MyDataBaseHelper;

import butterknife.ButterKnife;

public class SubjectAdd extends AppCompatActivity {


    EditText addText1,addText2,addText3,addText4,addText5,addText6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*导入界面*/
        setContentView(R.layout.activity_subject_add);
        ButterKnife.bind(this);
        Context context;

        //初始化数据库对象
        MyDataBaseHelper helper = new MyDataBaseHelper(this);//创建并初始化DBOpenHelper对
        final SQLiteDatabase db = helper.getWritableDatabase();//初始化SQLiteDatabase对象


        //导入按钮
        ImageButton backlist = findViewById(R.id.backList);
        Button submitBtn  = findViewById(R.id.submitBtn);

        //导入文本框
        addText1 = findViewById(R.id.addText1);
        addText2 = findViewById(R.id.addText2);
        addText3 = findViewById(R.id.addText3);

        //添加点击事件
        backlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(SubjectAdd.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });


        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String sub_1 = addText1.getText().toString();
                final String sub_2  = addText2.getText().toString();
                final String sub_3  = addText3.getText().toString();
                ContentValues values = new ContentValues();
                values.put("sub_1",sub_1);
                values.put("sub_2",sub_2);
                values.put("sub_3",sub_3);
                db.insert("subject", null, values);
                values.clear();
                Toast.makeText(SubjectAdd.this,"添加作业成功", Toast.LENGTH_SHORT).show();
                Intent intent2 = new Intent(SubjectAdd.this,MainActivity.class);
                startActivity(intent2);
                finish();
            }
        });
    }
}

